#ifndef _AS_SOCKET_H_
#define _AS_SOCKET_H_

#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <string>

#pragma comment(lib, "Ws2_32.lib")
#else
#include <sys/socket.h>
#include <string.h>
#endif

#include <memory>
#include "net_include.h"



class ByteBuffer;
typedef std::shared_ptr<ByteBuffer> ByteBufferPtr;

namespace as
{

namespace inet
{

class NetAddr;
class AsSocket
{
public:
    AsSocket();
    AsSocket(socket_fd fd);
    virtual ~AsSocket();

    int bind(NetAddr &netDetail);
    int connect(NetAddr &netDetail);
    int listen();
    socket_fd accept();

    int start_server(NetAddr &netDetail);

    inline socket_fd get_fd() { return m_fd; }
    inline void set_fd(socket_fd fd) { m_fd = fd; }

    static int recv(socket_fd fd, void *buf, int len, int flags = 0);
    int recv(ByteBufferPtr &buf);

    static void close(socket_fd fd);
    void close();

    static void set_non_block(socket_fd fd);
    void set_non_block();

    static void set_keep_alive(socket_fd fd);
    void set_keep_alive();

    static int get_last_err();
    
private:
    socket_fd m_fd;
};



}
}
#endif